package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
	"user-permission/cache"
	"user-permission/model"
	"user-permission/pkg"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户名或密码不能为空",
		})
		return
	}
	redis_key := "login_" + username
	ErrCount, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if ErrCount >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误三次，禁止登录",
		})
		return
	}

	var user model.User
	err := model.DB.Where("username=?", username).Find(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户查询失败",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码错误",
		})
		return
	}
	if user.Password != pkg.Md5(password) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if ErrCount == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误三次，禁止登录两小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": user,
	})
	return
}
func UserRoleAdd(c *gin.Context) {
	uid, _ := strconv.Atoi(c.PostForm("uid"))
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	data := model.UserRole{
		Uid: uid,
		Rid: rid,
	}
	model.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func RoleNodeAdd(c *gin.Context) {
	nid, _ := strconv.Atoi(c.PostForm("nid"))
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	data := model.RoleNode{
		Nid: nid,
		Rid: rid,
	}
	model.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func RoleList(c *gin.Context) {
	uid, _ := strconv.Atoi(c.PostForm("uid"))
	var role []model.Role
	//SELECT
	//*
	//	FROM
	//roles
	//LEFT JOIN user_roles ON user_roles.rid = roles.id
	//WHERE uid=1
	model.DB.Table("roles").
		Joins("LEFT JOIN user_roles ON user_roles.rid = roles.id").
		Where("uid=?", uid).Find(&role)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": role,
	})
	return
}
func NodeList(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	var node []model.Node
	//SELECT
	//*
	//	FROM
	//roles
	//LEFT JOIN user_roles ON user_roles.rid = roles.id
	//WHERE uid=1
	model.DB.Table("nodes").
		Joins("LEFT JOIN role_nodes ON role_nodes.nid = nodes.id").
		Where("rid=?", rid).Find(&node)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": node,
	})
	return
}
func UserList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))
	offset := (page - 1) * size

	var user []model.User
	redis_key := "list_" + strconv.Itoa(page)
	RedisData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if RedisData == "" {
		model.DB.Limit(size).Offset(offset).Find(&user)
		Redisjson, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, redis_key, Redisjson, time.Hour*2)
	} else {
		json.Unmarshal([]byte(RedisData), &user)
	}

	var total int64
	model.DB.Model(user).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  user,
		"total": total,
	})
	return
}
func UserDetail(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("id"))
	var user []model.User
	redis_key := "list_" + strconv.Itoa(id)
	RedisData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if RedisData == "" {
		model.DB.Where("id=?", id).Find(&user)
		Redisjson, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, redis_key, Redisjson, time.Hour*2)
	} else {
		json.Unmarshal([]byte(RedisData), &user)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": user,
	})
	return
}
